$(document).ready(function () {
    var platform = 'qiancheng'
    var pn = 1;

    function get_enterprise_list(init) {
        $.ajax({
            url: '/knx/get_enterprise_list',
            dataType: 'json',
            data: {
                page:pn,
                platform:platform
            },
            success: function (resp) {
                if (resp.success) {
                    var table_doc = "";

                    // 所有
                    var type_weights = {
                        '上市公司': 9,
                        '国企': 8,
                        '外资(欧美)': 7,
                        '外资(非欧美)': 6,
                        '合资': 5,
                        '民营公司': 4,
                        '创业公司': 3,
                        '外企代表处': 2,
                        '政府机关事业单位': 1,
                        '非营利组织': 0
                    }


                    if (resp.data) {
                        for (var i = 0; i < resp.data.length; i++) {
                            var item = resp.data[i];

                            table_doc += '<tr origin = "' + item.from + '">' +
                                '            <th scope = "row">' + (i + 1) + '</th>\n' +
                                '            <td class = "compName"><div>' + item.name + '</div></td>\n' +
                                '            <td style = "text-align: center;"><a target = "_blank" href = "' + item.site + '">' + (item.site ? '官网' : '-') + '</a></td>\n' +
                                '            <td>' + item.type + '</td>\n' +
                                '            <td>' + item.size + '</td>\n' +
                                '            <td>' + item.location.slice(0, 8) + '...' + '</td>\n' +
                                '            <td>' + item.industry + '</td>\n' +
                                '            <td class = "platform">' + item.platform + '</td>\n' +
                                '            <td><img style = "width:120px;" src = "' + item.logo_url +'"/></td>\n' +
//                                '            <td class = "del" style = "text-align: center;font-size:13px;cursor: pointer;"><a class = "mark_as_not_wanted">' + '回收' + '</a></td>\n' +
                                '        </tr>'
                        }
                        $('#company-container tbody').html(table_doc);

                        $("#page-container-static-normal").page({
                             count:resp.length,
                             pageSize:50,
                             pageNum:pn,
                             theme:"normal"
                        });

                        if(init){
                            $("#page-container-static-normal").on("pageChanged",function (event,params) {
                                   $(this).data("page").refresh(params);
                                   pn = Number(params.pageNum);
                                   get_enterprise_list();
                             });
                        }
                    } else {
                        $('#company-container tbody').html('<tr><td colspan = "9" style="text-align: center;">此列表下暂无相关企业</td></tr>');
                    }
                }
            }
        });
    }

    get_enterprise_list(true);

    $('#company-container').delegate('.mark_as_not_wanted','click',function(e){
        name = $(e.target).closest('tr').find('.compName').text();
        platform = $(e.target).closest('tr').find('.platform').text();

        mark_as_not_wanted(name,platform,function(){
            $(e.target).closest('tr').remove();
        });
    });

    function mark_as_not_wanted(name,platform,cb){
        $.ajax({
            url:'/knx/mark_as_not_wanted',
            dataType: 'json',
            data: {
                name:name,
                platform:platform
            },
            success: function (resp) {
                 if(resp.success){
                    cb && cb();
                 }
            }
        })
    }

    $('.plt').click(function(e){
        $(e.target).addClass('label-primary').siblings().removeClass('label-primary');

        platform = $(e.target).attr('data');
        pn = 1;

        get_enterprise_list();
    })
});